package com.xbs.dao.selfdriving;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.xbs.dao.conn.BaseDao;
import com.xbs.dao.conn.RowDatas;
/**
 * 自驾租车dao层
 * @author 余凯
 *
 */
import com.xbs.entity.CarModelEntity;
import com.xbs.entity.CityEntity;
import com.xbs.entity.StoreEntity;
import com.xbs.entity.TimeCombo;
import com.xbs.utils.StringUtils;
public class SelfDrivingDao extends BaseDao {
	//创建一个车型对象
	private RowDatas<CarModelEntity> carmodel;
	//创建一个城市对象
	private RowDatas<CityEntity> city;
	private RowDatas<Double> tc;
	private RowDatas<Integer> total;//总记录数
	public SelfDrivingDao(){
		carmodel = new getCarModel();
		city = new getCity();
		tc = new getTimeCombo();
		total = new getTotal();
	}

	
	/**
	 * 1.根据条件查找所需要的车型（默认查全部）
	 * @return
	 */
	public List<CarModelEntity> selCarModel(String type,Double price,Double price2,String pin,int currpage,int pagesize){
		StringBuffer sb =new StringBuffer("select * from tb_car_model");
		String suffix = " where 1=1";
		sb.append(suffix);
		//如果类型不为空
		if(StringUtils.isEmpty(type)){
			sb.append(" and car_model_id in (select car_model_id from tb_car where car_type ='"+type+"' and car_status=0) ");
		}
		//如果价格不为空
		if(StringUtils.isEmpty(price.toString())){
			sb.append(" and car_model_rent_cash between "+price+" and "+price2+"");
		}
		//如果品牌不为空
		if(StringUtils.isEmpty(pin)){
			sb.append(" and car_model_type = ");
			sb.append("'"+pin+"'");
		}
		sb.append(" limit "+currpage+","+pagesize+"");
		
		String sql = sb.toString();
		System.out.println(sql);
		return queryList(sql, carmodel);
	}
	
	/**
	 * 2.获取总的记录页数
	 * @param type
	 * @param price
	 * @param price2
	 * @param pin
	 * @return
	 */
	public int selCarModelTotal(String type,Double price,Double price2,String pin){
		StringBuffer sb =new StringBuffer("select count(*) from tb_car_model");
		String suffix = " where 1=1";
		sb.append(suffix);
		//如果类型不为空
		if(StringUtils.isEmpty(type)){
			sb.append(" and car_model_id in (select car_model_id from tb_car where car_type ='"+type+"')");
		}
		//如果价格不为空
		if(StringUtils.isEmpty(price.toString())){
			sb.append(" and car_model_rent_cash between "+price+" and "+price2+"");
		}
		//如果品牌不为空
		if(StringUtils.isEmpty(pin)){
			sb.append(" and car_model_type = ");
			sb.append("'"+pin+"'");
		}
		String sql = sb.toString();
		return query(sql, total);
	}
	
	/**
	 * 2.查找出拥有门店的城市
	 * @return
	 */
	public List<CityEntity> selCity(){
		String sql = "select * from tb_city where city_name in (select city_name from tb_city where parent_id in (select city_id from tb_city))";
		return queryList(sql, city);
	}
	
	/**
	 * 3.查找每种套餐的折扣
	 * @param time
	 * @return
	 */
	public Double getTimeCombo(String time){
		String sql="";
		if(time==null||time.length()==0||time.equals("短租")){
			sql ="select time_combo_short from tb_time_combo";
		}
		else if(time.equals("周租")){
			sql = "select time_combo_week from tb_time_combo";
		}else if(time.equals("月租")){
			sql = "select time_combo_month from tb_time_combo";
		}else if(time.equals("年租")){
			sql = "select time_combo_year from tb_time_combo";
		}
		
		return query(sql, tc);
	}
	
	/**
	 * 给车型对象赋值
	 * @author 余凯
	 *
	 */
	public class getCarModel implements RowDatas<CarModelEntity>{
		@Override
		public CarModelEntity getRowData(ResultSet rs) throws SQLException {
			CarModelEntity carmodel = new CarModelEntity();
			carmodel.setCarmodelname(rs.getString("car_model_name"));
			carmodel.setCarmodeltype(rs.getString("car_model_type"));
			carmodel.setCarmodelyear(rs.getString("car_model_year"));
			carmodel.setSeating(rs.getInt("car_model_seating"));
			carmodel.setDoornumber(rs.getInt("car_model_door_number"));
			carmodel.setFueltype(rs.getString("car_model_fuel_type"));
			carmodel.setCarmodelgps(rs.getString("car_model_gps"));
			carmodel.setCarmodelseat(rs.getString("car_model_seat"));
			carmodel.setCarmodelimage(rs.getString("car_model_image"));
			carmodel.setRentcash(rs.getDouble("car_model_rent_cash"));
			carmodel.setInsurance(rs.getDouble("car_model_insurance"));
			carmodel.setDiscount(rs.getDouble("company_discount"));
			carmodel.setPledge(rs.getDouble("car_model_pledge"));
			return carmodel;
		}
	}
	
	
	/**
	 * 获取城市对象
	 * @author 余凯
	 *
	 */
	public class getCity implements RowDatas<CityEntity>{
		@Override
		public CityEntity getRowData(ResultSet rs) throws SQLException {
			CityEntity city = new CityEntity();
			city.setCity_id(rs.getInt("city_id"));
			city.setCity_name(rs.getString("city_name"));
			city.setParent_id(rs.getInt("parent_id"));
			return city;
		}
	}
	
	public class getTimeCombo implements RowDatas<Double>{
		@Override
		public Double getRowData(ResultSet rs) throws SQLException {
			return rs.getDouble(1);
		}
	}
	
	
	public class getTotal implements RowDatas<Integer>{
		@Override
		public Integer getRowData(ResultSet rs) throws SQLException {
			return rs.getInt(1);
		}
	}
}
